package com.sugar.real.estate.module.business.controller.admin.buildhouse;

import com.sugar.real.estate.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.real.estate.framework.common.pojo.CommonResult;
import com.sugar.real.estate.framework.common.pojo.PageParam;
import com.sugar.real.estate.framework.common.pojo.PageResult;
import com.sugar.real.estate.framework.common.util.object.BeanUtils;
import com.sugar.real.estate.module.business.controller.admin.buildhouse.vo.BuildHousePageReqVO;
import com.sugar.real.estate.module.business.controller.admin.buildhouse.vo.BuildHouseRespVO;
import com.sugar.real.estate.module.business.controller.admin.buildhouse.vo.BuildHouseSaveReqVO;
import com.sugar.real.estate.module.business.dal.dataobject.buildhouse.BuildHouseDO;
import com.sugar.real.estate.module.business.service.buildhouse.BuildHouseService;
import com.sugar.real.estate.module.business.util.SelfExcelUtil;
import com.sugar.real.estate.module.infra.controller.admin.file.vo.file.FileUploadReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.real.estate.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.real.estate.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 建房")
@RestController
@RequestMapping("/business/build-house")
@Validated
public class BuildHouseController {

    @Resource
    private BuildHouseService buildHouseService;

    @PostMapping("/create")
    @Operation(summary = "创建建房")
    @PreAuthorize("@ss.hasPermission('business:build-house:create')")
    public CommonResult<Long> createBuildHouse(@Valid @RequestBody BuildHouseSaveReqVO createReqVO) {
        return success(buildHouseService.createBuildHouse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新建房")
    @PreAuthorize("@ss.hasPermission('business:build-house:update')")
    public CommonResult<Boolean> updateBuildHouse(@Valid @RequestBody BuildHouseSaveReqVO updateReqVO) {
        buildHouseService.updateBuildHouse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除建房")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:build-house:delete')")
    public CommonResult<Boolean> deleteBuildHouse(@RequestParam("id") Long id) {
        buildHouseService.deleteBuildHouse(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除建房")
                @PreAuthorize("@ss.hasPermission('business:build-house:delete')")
    public CommonResult<Boolean> deleteBuildHouseList(@RequestParam("ids") List<Long> ids) {
        buildHouseService.deleteBuildHouseListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得建房")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:build-house:query')")
    public CommonResult<BuildHouseRespVO> getBuildHouse(@RequestParam("id") Long id) {
        BuildHouseDO buildHouse = buildHouseService.getBuildHouse(id);
        return success(BeanUtils.toBean(buildHouse, BuildHouseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得建房分页")
    @PreAuthorize("@ss.hasPermission('business:build-house:query')")
    public CommonResult<PageResult<BuildHouseRespVO>> getBuildHousePage(@Valid BuildHousePageReqVO pageReqVO) {
        PageResult<BuildHouseDO> pageResult = buildHouseService.getBuildHousePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BuildHouseRespVO.class));
    }

    @PostMapping("/import-excel")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('business:build-house:export')")
    public CommonResult<Boolean> importExcel(FileUploadReqVO uploadReqVO) throws IOException {
        buildHouseService.importExcel(uploadReqVO);
        return success(true);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出建房 Excel")
    @PreAuthorize("@ss.hasPermission('business:build-house:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBuildHouseExcel(@Valid BuildHousePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BuildHouseDO> list = buildHouseService.getBuildHousePage(pageReqVO).getList();
        // 导出 Excel
        SelfExcelUtil.write(response, "建房.xls", "数据", BuildHouseRespVO.class,
                        BeanUtils.toBean(list, BuildHouseRespVO.class));
    }

}